﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DutchNationalFlagProblem
{
    public static class ArrayExtension
    {
        public static int[] dutchFlagSort(this int[] input, int low, int high)
        {
            int count = 0;

            int p = 0;
            int q = input.Length-1;

            for (int i = 0; i <= q; )
            {
                if (input[i] < low)
                {
                    Swap(input, i, p++);
                    i++;
                }
                else if (input[i] > high)
                    Swap(input, i, q--);
                else
                    i++;

                count++;
            }

            Console.WriteLine("Counter: {0}",count);

            return input;
        }

        private static int[] Swap(int[] input, int a, int b)
        {
            if (a != b)
            {
                input[a] ^= input[b];
                input[b] ^= input[a];
                input[a] ^= input[b];
            }

            return input;
        }
    }
}
